package Enum;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;

/**
 * @author: whh
 * @Date: 2021/5/31
 * @Description:
 */
public class enumTest {
    public static void main(String[] args) {
        Pizza pizza = new Pizza();
        pizza.setStatus(Pizza.PizzaStatus.READY);
        System.out.println(pizza.isDeliverable());

        /***
         * 由于枚举类型确保JVM中仅存在一个常量实例，因此我们可以安全地使用 == 运算符比较两个变量，
         * 如上例所示；此外，== 运算符可提供编译时和运行时的安全性。
         *
         * 首先，让我们看一下以下代码段中的运行时安全性，其中 == 运算符用于比较状态，
         * 并且如果两个值均为null 都不会引发 NullPointerException。
         * 相反，如果使用equals方法，将抛出 NullPointerException：
         */
        Pizza.PizzaStatus pizza1 = null;
        //System.out.println(pizza1.equals(Pizza.PizzaStatus.DELIVERED));
        System.out.println(pizza1 == Pizza.PizzaStatus.DELIVERED);

        Pizza testPz = new Pizza();
        testPz.setStatus(Pizza.PizzaStatus.ORDERED);
        testPz.printTimeToDeliver();

        givenPizaOrders_whenRetrievingUnDeliveredPzs_thenCorrectlyRetrieved();
        givenPizaOrders_whenGroupByStatusCalled_thenCorrectlyGrouped();
    }

    public static void givenPizaOrders_whenRetrievingUnDeliveredPzs_thenCorrectlyRetrieved() {
        List<Pizza> pzList = new ArrayList<>();
        Pizza pz1 = new Pizza();
        pz1.setStatus(Pizza.PizzaStatus.DELIVERED);

        Pizza pz2 = new Pizza();
        pz2.setStatus(Pizza.PizzaStatus.ORDERED);

        Pizza pz3 = new Pizza();
        pz3.setStatus(Pizza.PizzaStatus.ORDERED);

        Pizza pz4 = new Pizza();
        pz4.setStatus(Pizza.PizzaStatus.READY);

        pzList.add(pz1);
        pzList.add(pz2);
        pzList.add(pz3);
        pzList.add(pz4);

        List<Pizza> undeliveredPzs = Pizza.getAllUndeliveredPizzas(pzList);
        System.out.println(undeliveredPzs.size() == 2);
    }

    public static void givenPizaOrders_whenGroupByStatusCalled_thenCorrectlyGrouped() {
        List<Pizza> pzList = new ArrayList<>();
        Pizza pz1 = new Pizza();
        pz1.setStatus(Pizza.PizzaStatus.DELIVERED);

        Pizza pz2 = new Pizza();
        pz2.setStatus(Pizza.PizzaStatus.ORDERED);

        Pizza pz3 = new Pizza();
        pz3.setStatus(Pizza.PizzaStatus.ORDERED);

        Pizza pz4 = new Pizza();
        pz4.setStatus(Pizza.PizzaStatus.READY);

        pzList.add(pz1);
        pzList.add(pz2);
        pzList.add(pz3);
        pzList.add(pz4);

        EnumMap<Pizza.PizzaStatus,List<Pizza>> map = Pizza.groupPizzaByStatus(pzList);
        System.out.println(map.get(Pizza.PizzaStatus.DELIVERED).size() == 1);
        System.out.println(map.get(Pizza.PizzaStatus.ORDERED).size() == 2);
        System.out.println(map.get(Pizza.PizzaStatus.READY).size() == 1);
    }

}
